package com.ys.peony.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.AppManager;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.ui.widget.AlertTwoBtnDialog;
import com.ys.peony.ui.widget.CustomAlertDialogCallBack;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * describe:设置
 * author:zhangcong
 * date:2017/5/28 16:50
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView tvTitleName;

    @BindView(R.id.btn_logout)
    Button btnLogout;

    @BindView(R.id.modify_nickname)
    RelativeLayout modifyNickname;

    @BindView(R.id.tv_privacy)
    TextView tvPrivacy;

    @BindView(R.id.tv_about)
    TextView tvAbout;

    @Override
    protected int getContentResId() {
        return R.layout.activity_set;
    }


    @Override
    protected void initView() {
        tvTitleName.setText(R.string.label_setting);
        backBtn.setOnClickListener(this);
        modifyNickname.setOnClickListener(this);
        tvPrivacy.setOnClickListener(this);
        tvAbout.setOnClickListener(this);
        btnLogout.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.modify_nickname:
                ToastUtils.showToast("此功能正在开发中");
                break;
            case R.id.tv_privacy:
                ToastUtils.showToast("此功能正在开发中");
                break;
            case R.id.tv_about:
                skipToTargetPage(AboutActivity.class, null, SwipeDirect.DIRECT_LEFT);
                break;
            case R.id.btn_logout:
//                new MaterialDialog.Builder(this)
//                        .title(getString(R.string.logout_dialog_title))
//                        .positiveText(getString(R.string.label_sure))
//                        .negativeText(getString(R.string.label_cancle))
//                        .onPositive(new MaterialDialog.SingleButtonCallback() {
//                            @Override
//                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                logout();
//                            }
//                        })
//                        .show();
                AlertTwoBtnDialog dialog = new AlertTwoBtnDialog(this, new CustomAlertDialogCallBack() {
                    @Override
                    public void onCancelBtnClicked() {
                    }

                    @Override
                    public void onSureBtnClicked() {
                        logout();
                    }

                    @Override
                    public void onSureBtnClicked1(String editStr) {
                    }
                });
                dialog.setMessage(getString(R.string.logout_dialog_title));
                break;
            default:
                break;
        }
    }

    /**
     * describe:退出登录
     * author:zhangcong
     * date:2017/5/28 17:44
     */
    private void logout() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        httpManagerUtils.get(HttpURL.loginOut, params, new BaseRespCallBack(this) {
            public void onRespSuccess(String respStr) {
                SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_PWD,"");
            }
        });
        SharePreferencesUtils.getInstance().removeKey(YSConstant.USER_INFO);
        SharePreferencesUtils.getInstance().setUserModel(null);
        skipToTargetPage(LoginActivity.class, null, SwipeDirect.DIRECT_RIGHT);
        //AppManager.getAppManager().AppExit(BaseApplication.getInstance());
        AppManager.getAppManager().finishAllActivity();
    }
}
